package henu.graduation_design.onlineshop;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;

public class SettingsActivity extends Activity {

	ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_settings);

		imageView = (ImageView) findViewById(R.id.settings_imageView);
		SharedPreferences sp = getSharedPreferences("SP", MODE_PRIVATE);
		Boolean showAnimationBoolean = sp.getBoolean("IsAnima", true);
		if (showAnimationBoolean) {
			imageView.setImageDrawable(getResources().getDrawable(
					R.drawable.ic_settings_on));
		} else {
			imageView.setImageDrawable(getResources().getDrawable(
					R.drawable.ic_settings_off));
		}
		imageView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (imageView
						.getDrawable()
						.getConstantState()
						.equals(getResources().getDrawable(
								R.drawable.ic_settings_on).getConstantState())) {
					imageView.setImageDrawable(getResources().getDrawable(
							R.drawable.ic_settings_off));
					SharedPreferences sp = getSharedPreferences("SP",
							MODE_PRIVATE);
					// 存入数据
					Editor editor = sp.edit();
					editor.putBoolean("IsAnima", false);
					editor.commit();
				} else {
					imageView.setImageDrawable(getResources().getDrawable(
							R.drawable.ic_settings_on));
					SharedPreferences sp = getSharedPreferences("SP",
							MODE_PRIVATE);
					// 存入数据
					Editor editor = sp.edit();
					editor.putBoolean("IsAnima", true);
					editor.commit();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(this)
					// .setIcon(R.drawable.services)
					.setTitle("注意")
					.setMessage("确定要退出吗？")
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
								}
							})
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									finish();
								}
							}).show();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

}
